<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: Cruise <jiangyu@hrbjjhh.com>
// +----------------------------------------------------------------------------

namespace Admin\Model;
use Admin\Model\BaseModel;
/**
 * @todo 店铺类型模型
 * @author Cruise <jiangyu@hrbjjhh.com>
 */
class ShopCategoryModel extends BaseModel {

    /* 自动验证规则 */
    protected $_validate = array(
        array('shop_category_name', 'require', '店铺类型不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
    	array('shop_category_name', '1,10', '店铺类型长度不能超过10个字符', self::MUST_VALIDATE, 'length', self::MODEL_BOTH),
    	array('shop_category_name', 'checkName', '店铺品类已经存在', self::MUST_VALIDATE, 'callback', self::MODEL_BOTH),
    	array('sort', 'number', '排序必须是数字', self::MUST_VALIDATE, '', self::MODEL_BOTH),
    );

    /* 自动完成规则 */
    protected $_auto = array(
    	array('create_time', NOW_TIME, self::MODEL_INSERT),
    	array('update_time', NOW_TIME, self::MODEL_UPDATE),
    	array('status', '1', self::MODEL_INSERT),
    );
    
    /**
     * @todo 检测名称是否存在
     * @param  string $shop_category_name 品类名称
     * @return boolean     ture - 未重名，false - 已重名
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    protected function checkName($shop_category_name){
    	$shop_category_id = (int)I('shop_category_id');
    	//判断是否存在
    	$map = array();
    	if($shop_category_id != 0){
    		$map['shop_category_id'] = array('neq',$shop_category_id);
    	}
    	$map['shop_category_name'] = $shop_category_name;
    	$map['status'] = array('gt',-1);
    	if($this->where($map)->field('shop_category_id')->find()){
    		return false;
    	}
    	return true; 
    }

    /**
     * @todo 店铺类型详情,编辑时单条记录
     * @param int $shop_category_id 店铺类型id
     * @param string $field 查询字段
     * @return false|array
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function ShopCategoryShow($shop_category_id = 0,$field = true){//fang update
    	//初始化查询条件
    	$map = array();
    	//查询条件
    	$map['shop_category_id'] = $shop_category_id;
    	$map['status'] = array('gt',-1);
    	$info = $this->where($map)->field($field)->find();
    	if(empty($info)){
    		$this->error = '未查到品类信息！';
    		return false;
    	}
    	return $info;
    }
       
 	/**
     * @todo 删除店铺品类时判断是否绑定了店铺shop表
     * @param int $shop_category_id 店铺类型id
     * @return false 存在店铺品类与店铺的绑定  true 未绑定
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function judgeShopCategory($shop_category_id = 0){
    	if($shop_category_id == ''){
    		$this->error = '参数丢失，请重试';
    		return false;
    	}
    	$map['status'] = array('gt',-1);
    	$map['shop_category_id'] = array('in',$shop_category_id);
    	$count_list = M('Shop')->where($map)->field('shop_category_id')->select();
    	if(!empty($count_list)){
    		$this->error = '店铺品类已经绑定了店铺，请先删除店铺';
    		return false;
    	}
    	return true;
    }
    
    /**
     * @todo 返回主键
     * @see \Think\Model::getPk()
     * @author PHP@妖孽 <mashengjie@hrbjjhh.com>
     */
    public function getPk(){
    	return 'shop_category_id';
    }
}
